Virtual Machine |
The virtual-machine executes the vector of instructions produced by the compiler. The variable memory of the virtual machine is stored in some member variables of the class Cpl::VirtualMachine. These variables are:
|
Running the virtual machine |
To run code in the Wintempla virtual machine, you must:
|
Problem 1 |
Create a Wintempla dialog application called MyVirtualMachine to test the Wintempla virtual machine. Using Wintempla add two textboxes (tbxInput and tbxOutput) with the multi-line property, vertical scrollbars and horizontal scrollbar. Set the event of "Change" in the tbxInput textbox. Set also the "Want return" property in the tbxInput textbox. |
MyVirtualMachine.cpp |
. . . void MyVirtualMachine::tbxInput_Change(Win::Event& e) { wstring output; Cpl::Compiler compiler; vector<Cpl::Compiler::Instruction> machineCode; //_______________________________________________ Compile if (compiler.Compile(tbxInput.Text.c_str(), machineCode) == true) { compiler.ListCode(machineCode, output); } else { const int count = machineCode.size(); int line; for(int i = 0; i<count; i++) { if (machineCode[i].type == VM_ERROR) { line = machineCode[i].line_number; output += (wchar_t*)machineCode[i].name; break; } } //______________________ Display compilation errors tbxOutput.Text = output; return; } //________________________________________________ Execute the machine code Cpl::VirtualMachine virtualMachine; Mt::ThreadLink threadLink; wchar_t text[256]; virtualMachine.Setup(compiler.variableInfo, machineCode, L"dummy"); virtualMachine.ThreadFunc(threadLink); wchar_t * errorDescr = virtualMachine.GetErrorDescr(); //_______________________ Display execution errors if (errorDescr != NULL) { tbxOutput.Text = errorDescr; return; } //____________________________________ Display the double values const int doubleCount = virtualMachine.memDouble.size(); output.clear(); for(int i = 0; i < doubleCount; i++) { _snwprintf_s(text, 256, L"%s = %g\r\n", virtualMachine.GetVariableName(LEX_DATATYPE_DOUBLE, i).c_str(), virtualMachine.memDouble[i]); output += text; } tbxOutput.Text = output; //____________________________________ Display int values //. . . } |